﻿<Window x:Class="WpfHeadtrackingTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ht="clr-namespace:WpfHeadtracking;assembly=WpfHeadtracking"
    Title="WPF Headtracking Demo" Height="300" Width="300" WindowState="Maximized" WindowStyle="None" Topmost="False">
    <!-- It's highly recommended to use the app with WindowState="Maximized", WindowStyle="None", Topmost="True", which makes it full screen -->
    
    <!-- This is simply the app found in http://stuff.seans.com/2008/08/13/drawing-a-cube-in-wpf/ with a few changes and the Headtracking Viewport3D -->
    <Window.Resources>
        <!-- Uncomment the following line to activate the Wiimote provider, with a 24 inch monitor, using a head as dot distance -->
        <!-- <ht:WiimoteHeadtrackingProvider DotDistanceInMM="170" IsWiimoteAboveScreen="False" ScreenHeightInMM="320" x:Key="wiimoteHtProvider" /> -->
      
        <ht:MouseHeadtrackingProvider x:Key="mouseHtProvider"/>
    </Window.Resources>
    <Grid>
        <ht:Headtracking3DDecorator x:Name="ht3dd" HeadtrackingProvider="{StaticResource mouseHtProvider}" TargetPosition="0 0 3.5">
            <Viewport3D Name="viewport3D1">
                <Viewport3D.Camera>
                    <PerspectiveCamera x:Name="camMain" FieldOfView="45" FarPlaneDistance="100" NearPlaneDistance="0.05" Position="0 0 10" LookDirection="0 0 -3" />
                </Viewport3D.Camera>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                        </DirectionalLight>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelVisual3D>
                    <ModelVisual3D.Transform>
                        <Transform3DGroup>
                            <TranslateTransform3D OffsetX="-0.05" OffsetY="-0.05" OffsetZ="3.45" />
                        </Transform3DGroup>
                    </ModelVisual3D.Transform>
                    <ModelVisual3D.Content>
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D x:Name="meshMainTarget"  
                                 Positions="0 0 0  0.1 0 0  0 0.1 0  0.1 0.1 0  0 0 0.1  0.1 0 0.1  0 0.1 0.1  0.1 0.1 0.1"  
                                 TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                                </MeshGeometry3D>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.Material>
                                <DiffuseMaterial x:Name="matDiffuseMainTarget">
                                    <DiffuseMaterial.Brush>
                                        <SolidColorBrush Color="Black"/>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelVisual3D>
                    <ModelVisual3D.Content>
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D x:Name="meshMain"  
                                 Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1 1 1 1"  
                                 TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                                </MeshGeometry3D>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.Material>
                                <DiffuseMaterial x:Name="matDiffuseMain">
                                    <DiffuseMaterial.Brush>
                                        <SolidColorBrush Color="Red" Opacity="0.7"/>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
                <ModelVisual3D>
                    <ModelVisual3D.Transform>
                        <Transform3DGroup>
                            <ScaleTransform3D ScaleX=".5" ScaleY=".5" ScaleZ=".5" />
                            <TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="4" />
                        </Transform3DGroup>
                    </ModelVisual3D.Transform>
                    <ModelVisual3D.Content>
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D x:Name="meshMain2"  
                                 Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1"  
                                 TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                                </MeshGeometry3D>
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.Material>
                                <DiffuseMaterial x:Name="matDiffuseMain2">
                                    <DiffuseMaterial.Brush>
                                        <SolidColorBrush Color="Green" Opacity="0.7"/>
                                    </DiffuseMaterial.Brush>
                                </DiffuseMaterial>
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </ModelVisual3D.Content>
                </ModelVisual3D>
            </Viewport3D>
        </ht:Headtracking3DDecorator>
        <StackPanel Width="300" TextElement.FontSize="30" HorizontalAlignment="Left">
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  x:Name="positionX" Minimum="-20" Maximum="20" Value="0" />
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  x:Name="positionY" Minimum="-20" Maximum="20" Value="0" />
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  x:Name="positionZ" Minimum="-20" Maximum="20" Value="10" />
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"   x:Name="ldX" Minimum="-5" Maximum="5" Value="0" />
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  x:Name="ldY" Minimum="-5" Maximum="5" Value="0" />
            <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  x:Name="ldZ" Minimum="-5" Maximum="5" Value="-3" />
            <Slider x:Name="fieldOfView" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}"  Minimum="0" Maximum="180" Value="45" />

            <Label x:Name="headXLbl"/>
            <Label x:Name="headYLbl"/>
            <Label x:Name="headDistLbl"/>
            <Label x:Name="isHeadtracking"/>
            <Label x:Name="fldOfView" Content="{Binding ElementName=camMain, Path=FieldOfView}"/>
        </StackPanel>
    </Grid>
</Window>
